package com.cjb.usercenter.web;

import com.cjb.usercenter.entity.RpcInfoDO;
import com.cjb.usercenter.service.RpcDoService;
import com.cjb.usercenter.vo.ResultVo;
import com.cjb.usercenter.vo.enums.ResultEnum;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/rpc")
public class RpcController {

    private final static Logger logger = LoggerFactory.getLogger(RpcController.class);

    @Autowired
    private RpcDoService rpcDoService;


    @RequestMapping("/listRpcInfo")
    @ResponseBody
    public ResultVo listRpcInfo(HttpServletRequest request){
        ResultVo<List<RpcInfoDO>> ret = new ResultVo<>();
        try{
                    logger.info("【远程调用开始】,{}",request);
                   List<RpcInfoDO> list=rpcDoService.listRpcInfo();
                   ret.setMessage(ResultEnum.SUCCESS.getMessage());
                   ret.setCode(ResultEnum.SUCCESS.getCode());
                   ret.setT(list);
         }catch(Exception exception){
             logger.error("error: 请求服务出错 "+exception);
           ret.setT(null);
           ret.setCode(ResultEnum.ERROR.getCode());
           ret.setMessage(ResultEnum.ERROR.getMessage());
         }
        return ret;
    }



    @RequestMapping
    @ResponseBody
    public  ResultVo saveInfo(@RequestBody RpcInfoDO rpcInfoDO){
        ResultVo<Object> ret = new ResultVo<>();
        try{
            Integer info = rpcDoService.saveRpcInfo(rpcInfoDO);
            if(info>0){
                ret.setMessage(ResultEnum.SUCCESS.getMessage());
            }else{
                ret.setMessage(ResultEnum.ERROR.getMessage());
            }
        }catch(Exception exception){
             ret.setMessage(ResultEnum.SYS_FAIL.getMessage());
        }
        return ret;
    }





}
